// fast4j - Fast & Agile Service Tools for Java
// Copyright (C) 2007 Alexandre ROMAN
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

// $Id: MethodUtils.java 77 2007-04-14 13:58:46Z alexandre.roman $

package fast4j.ejbbridge.util;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * Utility methods for dealing with {@link Method} objects. This class is
 * <b>thread-safe</b>.
 * @author alexandre.roman
 */
public final class MethodUtils {
    private static final Map HASH_CACHE = new WeakHashMap();
    
    private MethodUtils() {
    }
    
    /**
     * Compute an unique identifier for a given method. Results from multiple
     * invocations are cached.
     * @param m input method
     * @return an unique identifier for this method
     */
    public static int hash(Method m) {
        Integer hash = null;
        
        synchronized (HASH_CACHE) {
            hash = (Integer) HASH_CACHE.get(m);
            if (hash == null) {
                hash = new Integer(m.toString().hashCode());
                HASH_CACHE.put(m, hash);
            }
        }
        return hash.intValue();
    }
}
